# Read the data file and run analyses.

setwd("F:/Experiment Data/Complex Contagion/ComplexContagionDatasets/")
exp1 = read.csv("Exp1_innocuous_questions.csv")

# Sanity checks.
head(exp1)
dim(exp1)
table(exp1$finished)
table(exp1$conditionName)

library(xtable)

# Code dependent variables.
# Code Self, Schis and Other columns.
exp1$Self = 0
exp1[exp1$userChoice == "Keep_10_cents_and_Do_Not_Donate", c("Self")] = 1
exp1$Schis = 0
exp1[exp1$userChoice == "Schistosomiasis_Control_Initiative", c("Schis")] = 1
exp1$Other = 0
exp1[exp1$Self == 0 & exp1$Schis == 0, c("Other")] = 1

# Code AnyCharity column.
exp1$AnyCharity = 0
exp1[exp1$userChoice != "Keep_10_cents_and_Do_Not_Donate", c("AnyCharity")] = 1

# Code independent variables.
# Code Tie Strength column.
exp1$Similarity = "High"
exp1[grepl("^AllLo__", exp1$conditionName), c("Similarity")] = "Low"
#exp1[, c("conditionName", "Similarity")]

# Code SelfNeighbors, SchisNeighbors and OtherNeighbors columns.
exp1$SelfNeighbors = 0
exp1[grepl("Self_1", exp1$conditionName), c("SelfNeighbors")] = 1
exp1[grepl("Self_2", exp1$conditionName), c("SelfNeighbors")] = 2
exp1[grepl("Self_3", exp1$conditionName), c("SelfNeighbors")] = 3

exp1$SchisNeighbors = 0
exp1[grepl("Schis_1", exp1$conditionName), c("SchisNeighbors")] = 1
exp1[grepl("Schis_2", exp1$conditionName), c("SchisNeighbors")] = 2
exp1[grepl("Schis_3", exp1$conditionName), c("SchisNeighbors")] = 3

exp1$OtherNeighbors = 0
exp1[grepl("Rand_1", exp1$conditionName), c("OtherNeighbors")] = 1
exp1[grepl("Rand_2", exp1$conditionName), c("OtherNeighbors")] = 2
exp1[grepl("Rand_3", exp1$conditionName), c("OtherNeighbors")] = 3

exp1$DonatingNeighbors = 3 - exp1$SelfNeighbors
#exp1$anyone = 3 - exp1$SelfNeighbors
#exp1$anytwo = exp1$SchisNeighbors + exp1$OtherNeighbors
#exp1$anynoo = exp1$anyone - exp1$anytwo

exp1[, c("conditionName", "Similarity", "SelfNeighbors", "SchisNeighbors", "OtherNeighbors", "DonatingNeighbors")]

# Raw numerator and denominator for AnyCharity.
aggregate(AnyCharity ~ DonatingNeighbors, exp1, sum)
exp1$countMe = 1
aggregate(countMe ~ DonatingNeighbors, exp1, sum)
table(exp1$DonatingNeighbors)



# Run regressions.
# Influence only model.
influenceModel = glm(Schis ~ SchisNeighbors, family=binomial(link='logit'), data=exp1)
summary(influenceModel)

# Influence with tie-strength interaction model.
influenceSimilarityModel = glm(Schis ~ SchisNeighbors * Similarity, family=binomial(link='logit'), data=exp1)
summary(influenceSimilarityModel)
xtable(summary(influenceSimilarityModel))

# Demographics only model.
demographicsModel = glm(Schis ~ gender + income + year_of_birth + smoking + drinking + mturk, family=binomial(link='logit'), data=exp1)
summary(demographicsModel)

# Influence + Tie Strength + Demographics model.
influenceDemographicsModel = glm(Schis ~ SchisNeighbors * Similarity + gender + income + year_of_birth + smoking + drinking + mturk, family=binomial(link='logit'), data=exp1)
summary(influenceDemographicsModel)


# Questions only model.
questionsModel = glm(Schis ~ cat_dog + outdoor_indoor + city_country + introvert_extrovert + team_individual + books_movies + scary_movies + cold_hot + coast_middle + scifi_mysteries, family=binomial(link='logit'), data=exp1)
summary(questionsModel)

# Influence + Tie Strength + Questions model.
influenceQuestionsModel = glm(Schis ~ SchisNeighbors * Similarity + cat_dog + outdoor_indoor + city_country + introvert_extrovert + team_individual + books_movies + scary_movies + cold_hot + coast_middle + scifi_mysteries, family=binomial(link='logit'), data=exp1)
summary(influenceQuestionsModel)

## Influence + Tie Strength + Demographics + Questions model.
kitchenSinkModel = glm(Schis ~ SchisNeighbors * Similarity + gender + income + year_of_birth + smoking + drinking + mturk + cat_dog + outdoor_indoor + city_country + introvert_extrovert + team_individual + books_movies + scary_movies + cold_hot + coast_middle + scifi_mysteries, family=binomial(link='logit'), data=exp1)
summary(kitchenSinkModel)
xtable(summary(kitchenSinkModel))


### Switch to Keeping Money for Self outcome and Self neighbor count predictor.
# Influence only model.
influenceModelSelf = glm(Self ~ SelfNeighbors, family=binomial(link='logit'), data=exp1)
summary(influenceModelSelf)

# Influence with tie-strength interaction model.
influenceSimilarityModelSelf = glm(Self ~ SelfNeighbors * Similarity, family=binomial(link='logit'), data=exp1)
summary(influenceSimilarityModelSelf)

# Demographics only model.
demographicsModelSelf = glm(Self ~ gender + income + year_of_birth + smoking + drinking + mturk, family=binomial(link='logit'), data=exp1)
summary(demographicsModelSelf)

# Influence + Tie Strength + Demographics model.
influenceDemographicsModelSelf = glm(Self ~ SelfNeighbors * Similarity + gender + income + year_of_birth + smoking + drinking + mturk, family=binomial(link='logit'), data=exp1)
summary(influenceDemographicsModelSelf)


# Questions only model.
questionsModelSelf = glm(Self ~ cat_dog + outdoor_indoor + city_country + introvert_extrovert + team_individual + books_movies + scary_movies + cold_hot + coast_middle + scifi_mysteries, family=binomial(link='logit'), data=exp1)
summary(questionsModelSelf)

# Influence + Tie Strength + Questions model.
influenceQuestionsModelSelf = glm(Self ~ SelfNeighbors * Similarity + cat_dog + outdoor_indoor + city_country + introvert_extrovert + team_individual + books_movies + scary_movies + cold_hot + coast_middle + scifi_mysteries, family=binomial(link='logit'), data=exp1)
summary(influenceQuestionsModelSelf)


### Switch to Donating to Any Charity outcome and DonatingNeighbors count predictor.
## Influence + Tie Strength + Demographics + Questions model.
kitchenSinkModelAnyCharity = glm(AnyCharity ~ DonatingNeighbors * Similarity + gender + income + year_of_birth + smoking + drinking + mturk + cat_dog + outdoor_indoor + city_country + introvert_extrovert + team_individual + books_movies + scary_movies + cold_hot + coast_middle + scifi_mysteries, family=binomial(link='logit'), data=exp1)
summary(kitchenSinkModelAnyCharity)
xtable(summary(kitchenSinkModelAnyCharity))

## Influence + Similarity with interaction model.
influenceSimilarityInteractionAnyCharity = glm(AnyCharity ~ DonatingNeighbors * Similarity, family=binomial(link='logit'), data=exp1)
summary(influenceSimilarityInteractionAnyCharity)
xtable(summary(influenceSimilarityInteractionAnyCharity))
